package com.company;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.io.Serializable;

/**
 * @date : 2022/7/26 14:35
 * 使用isBoolean定义布尔类型的序列化问题
 * https://mp.weixin.qq.com/s/bhCMqJnms3Q84kVvh_cd4g
 */

public class P025_IsBooleanSerializable {


    public static void main(String[] args) throws JsonProcessingException {
        Model model = new Model();
        model.setSuccess(true);

        // 使用 fastjson 序列化
        System.out.println(JSON.toJSONString(model));
        System.out.println(JSON.parseObject(JSON.toJSONString(model), Model.class));

        // 使用 gson 序列化
        Gson gson = new Gson();
        // 使用 fastjson 序列化结果再用 gson 反序列化时，与预期结果不一致
        System.out.println(gson.fromJson(JSON.toJSONString(model), Model.class));
        System.out.println(gson.toJson(model));
        System.out.println(gson.fromJson(gson.toJson(model), Model.class));

        // 使用 jackson 序列化
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(model));
    }


    public static class Model implements Serializable {

        private boolean isSuccess;

        public boolean isSuccess() {
            return isSuccess;
        }

        public void setSuccess(boolean success) {
            this.isSuccess = success;
        }

        public String getName() {
            return "hello";
        }

        @Override
        public String toString() {
            return "Model{" +
                    "isSuccess=" + isSuccess +
                    '}';
        }
    }
}
